package com.tm.service.impl;

import com.tm.mapper.PayMemberMapper;
import com.tm.service.PayMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tm.pojo.PayPojo;
import tm.pojo.PrivilegePojo;
import tm.pojo.RechargePojo;
import tm.pojo.UserPojo;
import tm.pojo.vo.UserBeMemberLogVo;
import tm.result.Result;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Rocky
 * @create 2022-09-28 17:03
 */
@Service
public class PayMemberServiceImpl implements PayMemberService {

    @Autowired
    private PayMemberMapper payMemberMapper;

    @Override
    public Result queryPrivilege() {
        List<PrivilegePojo>list=payMemberMapper.queryPrivilege();
        return Result.SUCCESS(list);
    }

    @Override
    public Result payPrivilege(PrivilegePojo privilegePojo) {

        Integer privilegeId = privilegePojo.getPrivilegeId();
        PrivilegePojo privilegePojo1= payMemberMapper.queryPrivilegeByid(privilegeId);
        BigDecimal privilegePrice = privilegePojo1.getPrivilegePrice();
        Integer privilegeType = privilegePojo1.getPrivilegeType();
        List<UserPojo>userPojoList=privilegePojo.getUserPojoList();
        PayPojo payPojo = new PayPojo();
        for (int i = 0; i < userPojoList.size(); i++) {
            UserPojo userPojo = userPojoList.get(i);
            Integer userId = userPojo.getUserId();
            payPojo.setPrivilegeId(privilegeId);
            payPojo.setUserId(userId);
            payPojo.setCreateTime(new Date().getTime()/1000);
            payPojo.setPrivilegePrice(privilegePrice);
            payPojo.setPrivilegeType(privilegeType);
            PayPojo pojo= payMemberMapper.queryPay(userId);
            if (pojo!=null){
                return Result.ERROR(999,"已存在该订单,请先支付");
            }
        }


        payMemberMapper.addPay(payPojo);

        return Result.SUCCESS();
    }
//
    @Override
    public Result payMember(PayPojo payPojo) {
        Integer userId = payPojo.getUserId();
        PayPojo pojo= payMemberMapper.queryPay(userId);
        if (pojo==null){
            return Result.ERROR(998,"当前订单不存在请重新下单");
        }
        Integer privilegeId = pojo.getPrivilegeId();
        BigDecimal privilegePrice1 = payPojo.getPrivilegePrice();
        BigDecimal privilegePrice = pojo.getPrivilegePrice();
        if (privilegePrice1.equals(privilegePrice)){
                RechargePojo rechargePojo = new RechargePojo();
                rechargePojo.setUserId(userId);
                rechargePojo.setPrivilegeId(privilegeId);
                rechargePojo.setRechargeTime(new Date().getTime()/1000);
                if (pojo.getPrivilegeType()==1){
                    rechargePojo.setExpireTime(1666959900L);
                }if (pojo.getPrivilegeType()==2){
                    rechargePojo.setExpireTime(1664899200L);
                }
                payMemberMapper.addRech(rechargePojo);
                payMemberMapper.deletePayByUserId(userId);
                return Result.SUCCESS();
        }else {
            return Result.ERROR(999,"未付指定金额，无法购买");
        }
    }

    @Override
    public List<UserBeMemberLogVo> queryUserBeMemberLog(Integer userId) {
        List<UserBeMemberLogVo>list=payMemberMapper.queryUserBeMemberLog(userId);
        return list;
    }

}
